<?php
ob_start();
session_start();

$id=$_SESSION['id'];

try{

include_once('restconnection.php'); 
     
     if(isset($client)){  
      
        //retrive base url 
        $url = $_SESSION['basic_url'];
        //add service url
        $service_url="/InteractionRate?";
        //params url
        $params = array('id'=>$id); 
        //params connection
        $ctx = stream_context_create(array('http' => array('timeout' => 5))); 
        //send GET request
        $final_url = $url.$service_url."id=".$params['id'];
        //print_r($final_url); break;
        $output = @file_get_contents($final_url,0,$ctx);
        //parse JSON output
        //print_r($url.$service_url."id=".$params['id']); break;
        $data = json_decode($output);
        //var_dump($output); break;
        $webResult = $data->InteractionRateResult;
       // print_r($webResult); break;
        

        //split and riorganize Facebook message_post  
        if($webResult[0]!= null){   
        
             $get_posts=$webResult;
              //number of all records devono essere 11
             $num_records=count($get_posts);
            // echo$num_records; 
            // break;
         
             if($num_records!=1){
                for($i=0;$i<30;$i++){
                   if($get_posts[$i]!=null){
                       
                        $split = explode("+-+",$get_posts[$i]);
                        
                        $message[$i]=$split[0];   
                       // $message[$i]=message_convert_url($mess[$i]);
                        
                        $datetime[$i]=$split[1];
                        $datetime[$i]=substr($datetime[$i], 0, -3);
                        $datetime[$i]=str_replace(".",":",$datetime[$i]);
                        
                        $socialnet[$i]=$split[2];
                        
                        $n_likes[$i]=$split[3];
                        $n_comments[$i]=$split[4];
                        $n_shares[$i]=$split[5];
                        
                        $interaction_rate[$i]=$split[6];
                        
                        $data_elaborate[$datetime[$i]] = $interaction_rate[$i];
                                      
                   }
  
                }//for
                
                     // print_r($data_elaborate);  
             
             }else{
                
                        $split = explode("+-+",$get_posts[0]);
                        
                        $mess[0]=$split[0];   
                        $message[0]=message_convert_url($mess[0]);
                        
                        $datetime[0]=$split[1];
                        $datetime[0]=substr($datetime[$i], 0, -3);
                        $datetime[0]=str_replace(".",":",$datetime[0]);
                        
                        $socialnet[0]=$split[2];
                        
                        $n_likes[0]=$split[3];
                        $n_comments[0]=$split[4];
                        $n_shares[0]=$split[5];
                        
                        $interaction_rate[0]=$split[6];
                        
                        $data_elaborate[$datetime[0]] = $interaction_rate[0];
              }    
?>
<head>
    <title>Interaction</title>
    <link rel="stylesheet" type="text/css" href="css/table_css.css">
</head>

<body style="background-color: white; text-align: center"
<label>STATISTIC FOR YOUR CONNECTED SOCIAL NETWORK!</label>
<br /><br />

<a href="general_stats.php" ><label>Panoramic view </label></a><strong><label>| Interaction </label></strong> <a href="engagement.php" ><label>| Engagement </label></a> <a href="passion.php" ><label>| Passion </label></a>

<br/> <br/> 

<label style="font-size: 30px;">Interaction</label> <br />

 
<br />

<?php $targetUrl = 'interaction_graph.php?';

for($i=1;$i<=30;$i++){
   
    $targetUrl .= $i . "=" .$interaction_rate[$i-1].'&'; 
    
   
    
}

$targetUrl = substr($targetUrl, 0, -1); 


echo "<img src='$targetUrl'>";  ?>

<br />

<label>Number of Posts</label>
<br /><br /><br />

<label> MORE DETAILS</label>
<br />

<div class="CSSTableGenerator" >
<table width="100%" border="1" cellpadding="10" cellspacing="1">
    <tr>
        <td width="5%">Id post</td>
        <td>Social</td>
        <td>Data</td>
        <td>Post</td>
        <td title="# of likes for each post"># of likes?</td>
        <td title="# of comments for each post"># of comments?</td>
        <td title="# of shares for each post"># of shares?</td>
        <td title="Amount of interaction that generates a single post on a social network. Formulas is # of like + # of comments performed on post ">
            Interaction</td>
        </tr>

<?php


    

                
                for($i=0;$i<30;$i++){
                
                    if($get_posts[$i]!=null){
        ?>          <tr> 
                    <td><?php echo$i+1; ?></td>
                    <td>
                         <?php 
                            if($socialnet[$i] == "Facebook") echo"<img style='width:30px;'  src='img/fb_icon.png' title='$socialnet[$i]' alt='$socialnet[$i]' />";
                            if($socialnet[$i] == "Google")  echo"<img style='width:30px;'   src='img/g+_icon.png' title='$socialnet[$i]' alt='$socialnet[$i]' />";
                            if($socialnet[$i] == "Twitter") echo"<img  style='width:30px;'  src='img/tw_icon.png' title='$socialnet[$i]' alt='$socialnet[$i]' />";
                            if($socialnet[$i] == "Pinterest")  echo"";
                            if($socialnet[$i] == "Linkedin")  echo"";
                            if($socialnet[$i] == "Foursquare") echo"";    
                            
                        ?> 
                    </td>
                    <td><?php echo$datetime[$i];?></td>
                    <td><?php echo$message[$i];?></td>
                    <td><?php echo$n_likes[$i]; ?></td> 
                    <td><?php echo$n_comments[$i]; ?></td> 
                    <td><?php echo$n_shares[$i]; ?></td> 
                    <td><?php echo$interaction_rate[$i]; ?></td> 
                
                    </tr>
        <?php
                   }//if
                }//for
                
  
                                 
          }else{
              ?>
              <label style="margin-left:0px;">There are no posts</label>
            
              <?php  
          }
            
     }//isset(client)  
     
}catch(Exeption $ex){
    
    
    
}
   
            
  ?>  

 
</table>
</div>    




</body>

